<?php
namespace App\Http\Controllers;
/**
 * 
 * 旅游线路控制器
 * 
 * @author shengl
 *
 */
use App\Models\Article;
use App\Services\BaiduMapService;
use App\Services\TourService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class TourController extends BaseController {

//    /**
//     *
//     * @param firstrow
//     * @param pagesize
//     *
//     * @return json
//     */
//    public function getList(){
//
//        $firstrow = Input::get('firstrow',0);
//        $pagesize = Input::get('pagesize',10);
//
//        $data = [];
//
//        return $this->success($data);
//
//    }

    /**
     *
     * 路线详情
     *
     * @param $tour_id 拼团ID
     * @param TourService $tourService
     * @return mixed
     */
    public function getDetail($tour_id,TourService $tourService){

        //参数编辑
        $tour_id = intval($tour_id);
        $user_id = intval(Input::get('user_id',0));
        if (!$tour_id) {
            return $this->args_invalid();
        }

        //获取拼团数据
        $tourDetail = $tourService->getTourDetail($tour_id,$user_id,true);

        if(!$tourDetail['result']){
            return $this->failure($tourDetail['msg']);
        }
        $service = new TourService();
        $comments = $service->getComments($tour_id,0,1);

        $tourInfo = $tourDetail['data'];
        $tourInfo['comments'] = $comments;
        /* 更新浏览次数 */
        $tourService->update_statistics($tour_id,'views');

        //编辑结果
        return $this->success($tourInfo);
    }

    /**
     * 根据月份获取线路日程
     * @param $tour_id
     * @param TourService $tourService
     * @return mixed
     */
    public function getScheduleByMonth($tour_id,TourService $tourService){
        $buy_type = Input::get('buy_type',TOUR_ORDER_EXTENSION_NORMAL);
        $month = Input::get('month','');

        if (!$tour_id || !$month) {
            return $this->args_invalid();
        }

        $monthSchedule = [];
        $scheduleByMonth = $tourService->getScheduleByMonth($tour_id,$buy_type,$month);

        if($scheduleByMonth['result']){
            $monthSchedule = $scheduleByMonth['data'];
        }

        $data = new \stdClass;
        foreach($monthSchedule as $item){
            $date = local_date('j',$item->open_date);
            $data->$date = $item;
        }

        return $this->success($data);

    }

    //收藏拼团
    /**
     * 收藏拼团
     */
    public function collect($pintuan_id){

        $collect_status=0;

        $user_id = Input::get('user_id');

        //参数编辑
        if (!$pintuan_id || !$user_id){
            return $this->args_invalid();
        }
        
        /* 验证要收藏的拼团是否存在 */
        $count =DB::table('pintuan')
                    ->where('pintuan_id',$pintuan_id)
                    ->count();

        if(!$count){
            return $this->failure('该拼团不存在');
        }

        DB::beginTransaction();
        
        try {
            /* 更新收藏表 */   
            unset($count); 
            $count = DB::table('collect')
                        ->where('user_id',$user_id)
                        ->where('type','pintuan')
                        ->where('item_id',$pintuan_id)->count();
            $message = "收藏成功";
            if(!$count){
                DB::table('collect')->insert(
                    array(
                        'user_id' => $user_id,
                        'type' => 'pintuan',
                        'item_id'=>$pintuan_id,
                        'keyword'=>'',
                        'add_time' =>gmtime())
                );
                
                $collect_status=1;
            } else {
                DB::table('collect')
                    ->where('user_id',$user_id)
                    ->where('type','pintuan')
                    ->where('item_id',$pintuan_id)->delete();
                    
                $collect_status=0;
                $message = "取消收藏";
            }
             
            /* 更新被收藏次数 */           
            unset($count);
            $count = DB::table('collect')
                ->where('type','pintuan')
                ->where('item_id',$pintuan_id)
                ->count();
                
            DB::table('pintuan_statistics')
               ->where('pintuan_id',$pintuan_id)
               ->update(array('collects'=>$count));       
      
            DB::commit();

            return $this->success(['collect_status'=>$collect_status],$message);

        } catch (Exception $e) {
            DB::rollback();
            throw $e;
        }            
    }

    /**
     *
     * 获取常见问题
     *
     * @param $pintuan_id
     *
     * @return mixed
     */
    public function getFqa($pintuan_id){

        $data=['content'=>''];

        $article =Article::find(config('maqu.ARTICLD_ID_FQA',18));
        if($article){

            $html =complete_html(config('maqu.STATIC_RESOURCE_URL'),$article->title,$article->content);
            $data['content']=$html;
            unset($html);
        }

        return $this->success($data);

    }

    /**
     * 根据线路主题获取线路
     *
     * @param $cate_id 线路分类
     * @param TourService $tourService 线路服务
     * @param BaiduMapService $mapService 地图服务服务
     * @return mixed
     */
    public function getToursByCate($cate_id,TourService $tourService,BaiduMapService $mapService){

        //参数
        //【经纬度】
        //    lat   经度
        //    lon   维度
        //【查询等级】
        //    level city or country
        //【排序】
        //  sort_type 排序方式 price or sales or distance or views
        //  sort    升降序 asc or desc
        //【位置】
        // country 0-全部 or 选择的县名
        //【翻页】
        //firsstrow
        //pagesize

        $lat =Input::get('lat',999);
        $lon =Input::get('lon',999);
        $sort_type =Input::get('sort_type','price');
        $sort =Input::get('sort','asc');
        $firstrow =Input::get('firstrow',0);
        $pagesize =Input::get('pagesize',10);
//		$cityname = Input::get('cityname');
        $province = Input::get('province');
        $city = Input::get('city');
        $country =Input::get('district');
        if(empty($country)){
            $country =Input::get('country');
        }
		if($lat=='undefined'){
			$lat = 999;
		}
		if($lon=='undefined'){
			$lon = 999;
		}
		
//        $location = $mapService->locationByGPS($lon,$lat);
//        if(!$location){
//            return $this->failure('根据定位获取城市失败。');
//        }

//        $data = $tourService->getToursByCate($cate_id,$lat,$lon,$level,$location['province'],$location['city'],$country,$sort_type,$sort,$firstrow,$pagesize);
		$data = $tourService->getToursByCate($cate_id,$lat,$lon,$province,$city,$country,$sort_type,$sort,$firstrow,$pagesize);
        if(!$data['result']){
            return $this->failure($data['msg']);
        }
        return $this->success($data['data']);
    }

    /**
     * 获取线路的评价列表
     *
     * @param $tour_id
     * @param TourService $tourService 线路服务
     * @return mixed
     */
    public function getComments($tour_id,TourService $tourService){

        $firstrow =Input::get('firstrow',0);
        $pagesize =Input::get('pagesize',10);

        if(!$tour_id){
            return $this->args_invalid();
        }

        $data = $tourService->getComments($tour_id,$firstrow,$pagesize);
        if(!$data['result']){
            return $this->failure($data['msg']);
        }

        return $this->success($data['data']);

    }
    
}
